const platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios

const checkNotifyMethodByPlatform = () => {
	if (platform === "ios") {
		return () => {
			let UIApplication = plus.ios.import("UIApplication");
			let app = UIApplication.sharedApplication();
			let enabledTypes = 0;
			if (app.currentUserNotificationSettings) {
				let settings = app.currentUserNotificationSettings();
				enabledTypes = settings.plusGetAttribute("types");
				console.log("enabledTypes1:" + enabledTypes);
				if (enabledTypes == 0) {
					//如果enabledTypes = 0 就是通知权限没有开启
					return false;
				} else {
					return true;
				}
			}
			plus.ios.deleteObject(settings);
		};
	} else {
		return () => {
			console.log("我是安卓", plus.android);
			let main = plus.android.runtimeMainActivity();
			let pkName = main.getPackageName();
			let uid = main.getApplicationInfo().plusGetAttribute("uid");
			let NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
			//android.support.v4升级为androidx
			if (NotificationManagerCompat == null) {
				NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
			}
			let areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
			console.log(areNotificationsEnabled);
			// 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
			if (!areNotificationsEnabled) {
				return false; //这里也一样未开启权限，弹出弹窗
			}
			if (!areNotificationsEnabled) {
				return false;
			}

			return true;
		};
	}
};

const openNotifyMethodByPlatform = () => {
	if (platform === "ios") {
		return () => {
			//如果机型是ios，ios由于权限问题，可能需要手动开启
			let UIApplication = plus.ios.import("UIApplication");
			let app = UIApplication.sharedApplication();
			let settings = app.currentUserNotificationSettings();
			let enabledTypes = settings.plusGetAttribute("types");
			let NSURL2 = plus.ios.import("NSURL");
			let setting2 = NSURL2.URLWithString("app-settings:");
			let application2 = UIApplication.sharedApplication();
			application2.openURL(setting2);
			plus.ios.deleteObject(setting2);
			plus.ios.deleteObject(NSURL2);
			plus.ios.deleteObject(application2);
			plus.ios.deleteObject(settings);
		};
	} else {
		return () => {
			//如果机型是安卓
			let main = plus.android.runtimeMainActivity();
			let pkName = main.getPackageName();
			let uid = main.getApplicationInfo().plusGetAttribute("uid");
			let Intent = plus.android.importClass("android.content.Intent");
			let Build = plus.android.importClass("android.os.Build");
			//android 8.0引导
			if (Build.VERSION.SDK_INT >= 26) {
				//判断安卓系统版本
				let intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
				intent.putExtra("android.provider.extra.APP_PACKAGE", pkName);
			} else if (Build.VERSION.SDK_INT >= 21) {
				//判断安卓系统版本
				//android 5.0-7.0
				let intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
				intent.putExtra("app_package", pkName);
				intent.putExtra("app_uid", uid);
			} else {
				//(<21)其他--跳转到该应用管理的详情页
				intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
				let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
				intent.setData(uri);
			}
			// 跳转到该应用的系统通知设置页
			main.startActivity(intent);
		};
	}
};

const checkNotifyPermission = checkNotifyMethodByPlatform();

const openNotifyPermission = openNotifyMethodByPlatform();

export { checkNotifyPermission, openNotifyPermission };
